package cn.com.shopec.erp.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.base.dao.ISupplierInfoDao;
import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.product.dao.ICommodityDao;
import cn.com.shopec.erp.product.dao.INventoryCheckDao;
import cn.com.shopec.erp.product.dao.IProductDao;
import cn.com.shopec.erp.product.dao.IProductStockDao;
import cn.com.shopec.erp.product.entity.InventoryCheck;
import cn.com.shopec.erp.product.entity.ProductStock;
import cn.com.shopec.erp.product.entity.ProductStockExt;
import cn.com.shopec.erp.product.service.IProductStockService;
import cn.com.shopec.erp.product.vo.ProductStockVo;

@Service
public class ProductStockServiceImpl implements IProductStockService {

	@Resource
	private ICommodityDao commodityDao;

	@Resource
	private IProductDao productDao;

	@Resource
	private IProductStockDao productStockDao;

	@Resource
	private ISupplierInfoDao supplierInfoDao;
	
	@Resource
	private INventoryCheckDao inventoryCheckDao;

	@Override
	public PageFinder<ProductStockExt> productStockPaged(ProductStockVo productStockVo, PageQuery pageQuery) {

		return productStockDao.productStockPaged(productStockVo, pageQuery);
	}

	@Override
	public PageFinder<ProductStockExt> productStockDefect(ProductStockVo productStockVo, PageQuery pageQuery) {

		return productStockDao.productStockDefect(productStockVo, pageQuery);
	}
	
	@Override
	public int getProductCountByWhNo(String whNo) {
		DetachedCriteria detachedCriteria = productStockDao.createCriteria();
		detachedCriteria.add(Restrictions.eq("whNo", whNo));
		return productStockDao.getCountByCriteria(detachedCriteria);
	}

	@Override
	public List<Map<String, Object>> checkProductStock(List<Object> excelData) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for (Object obj : excelData) {
			ProductStock ps = (ProductStock) obj;
			String supAccount = ps.getSupNo();
			SupplierInfo supplierInfo = supplierInfoDao.findUniqueBy("account", supAccount);
			if (supplierInfo != null) {
//				ps.setSupNo(supplierInfo.getAccount());
				ps.setSupNo(supplierInfo.getCode());
			}
			ProductStock sysPs = productStockDao.findUniqueProduct(ps);
			Map<String, Object> map = new HashMap<String, Object>();
			if (sysPs != null) {
				if (!sysPs.getProductTotal().equals(ps.getProductTotal())
						|| !sysPs.getQualifiedQuantity().equals(ps.getQualifiedQuantity())
						|| !sysPs.getDefectiveQuantity().equals(ps.getDefectiveQuantity())) {
					map.put("sysProductTotal", sysPs.getProductTotal());
					map.put("sysQualifiedQuantity", sysPs.getQualifiedQuantity());
					map.put("sysDefectiveQuantity", sysPs.getDefectiveQuantity());
					convertToMap(ps, map);
					map.put("id", sysPs.getId());
					map.put("isExist", 1);
					map.put("supAccount", supAccount);
					map.put("uniqueId", new Date().getTime());
					resultList.add(map);
				}
			} else {
				convertToMap(ps, map);
				map.put("id", new Date().getTime());
				map.put("isExist", 0);
				map.put("supAccount", supAccount);
				map.put("uniqueId", new Date().getTime());
				resultList.add(map);
			}
		}
		return resultList;
	}

	@Override
	@Transactional
	public String synchronizationToSys(List<ProductStock> productStockList) {
		try {
			for (ProductStock ps : productStockList) {
				ProductStock finded = productStockDao.findUniqueBy("id", ps.getId());
				ps.setUpdateTime(new Date());
				ps.setVipOccupyQuantity(0L);
				if (finded == null) {
					ps.setId(null);
					//增加盘点记录
					inventoryCheckDao.save(ProductStockToInventoryCheck(ps,finded));
					productStockDao.save(ps);
				} else {
					//修改盘点记录
					inventoryCheckDao.save(ProductStockToInventoryCheck(ps,finded));
					ps.setPreOccupyQuantity(finded.getPreOccupyQuantity());
					productStockDao.update(ps);
				}
			}
			return SZSCMConstant.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SZSCMConstant.FAIL;
	}
	
	/***
	 * 库存盘点转换器
	 * @param productStock
	 * @return
	 */
	public InventoryCheck ProductStockToInventoryCheck(ProductStock productStock,ProductStock finded){
		InventoryCheck inventory = new InventoryCheck();
		if(finded!=null){
			inventory.setpId(productStock.getId());
			inventory.setWhNo(productStock.getWhNo());
			inventory.setSupNo(productStock.getSupNo());
			inventory.setProdNo(productStock.getProdNo());
			inventory.setCommoNo(productStock.getCommoNo());
			inventory.setQualifiedQuantity(productStock.getQualifiedQuantity());
			inventory.setCreateTime(DateUtil.formatTimesTampDate(new Date()));
			inventory.setDeleteFlage(ProductSCMConstant.INVENTORY_CHECK_CREATE);
			inventory.setSysProduct(finded.getProductTotal());//系统总数
			inventory.setSysDefectProduct(finded.getDefectiveQuantity());//系统次品
			inventory.setSysYieldProduct(finded.getQualifiedQuantity());//系统良品
			inventory.setIsSysData(ProductSCMConstant.IS_SYS_DATA);
			inventory.setDefectProduct(productStock.getDefectiveQuantity());//次品
			inventory.setYieldProduct(productStock.getQualifiedQuantity());//良品
			inventory.setProductTotal(productStock.getProductTotal());//总数
		}else{
			inventory.setpId(productStock.getId());
			inventory.setWhNo(productStock.getWhNo());
			inventory.setSupNo(productStock.getSupNo());
			inventory.setProdNo(productStock.getProdNo());
			inventory.setCommoNo(productStock.getCommoNo());
			inventory.setQualifiedQuantity(productStock.getQualifiedQuantity());
			inventory.setCreateTime(DateUtil.formatTimesTampDate(new Date()));
			inventory.setDeleteFlage(ProductSCMConstant.INVENTORY_CHECK_CREATE);
			inventory.setIsSysData(ProductSCMConstant.I_SYS_DATA);
			inventory.setDefectProduct(productStock.getDefectiveQuantity());//次品
			inventory.setYieldProduct(productStock.getQualifiedQuantity());//良品
			inventory.setProductTotal(productStock.getProductTotal());//总数
		}
		return inventory;
	}
	
	private void convertToMap(ProductStock productStock, Map<String, Object> map) {
		map.put("id", productStock.getId());
		map.put("whNo", productStock.getWhNo());
		map.put("supNo", productStock.getSupNo());
		map.put("prodNo", productStock.getProdNo());
		map.put("commoNo", productStock.getCommoNo());
		map.put("productTotal", productStock.getProductTotal() != null ? productStock.getProductTotal() : 0);
		map.put("qualifiedQuantity",
				productStock.getQualifiedQuantity() != null ? productStock.getQualifiedQuantity() : 0);
		map.put("defectiveQuantity",
				productStock.getDefectiveQuantity() != null ? productStock.getDefectiveQuantity() : 0);
		map.put("preOccupyQuantity",
				productStock.getPreOccupyQuantity() != null ? productStock.getPreOccupyQuantity() : 0);
	}

	@Override
	public ProductStock getProductStock(ProductStock productStock) throws Exception {
		return productStockDao.getById(productStock.getId());
	}

	@Override
	@Transactional
	public String setVipOccupyQuantity(ProductStock productStock) {
		if(productStock.getVipOccupyQuantity() != null || productStock.getId() != null){
			if(productStockDao.setVipOccupyQuantity(productStock.getId(),productStock.getVipOccupyQuantity())){
				return SZSCMConstant.SUCCESS;
			}
		}
		return SZSCMConstant.FAIL;
	}

}
